package com.project.shiro;

import com.project.entity.User;
import com.project.service.UserService;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

/**
 * Author TateBrown
 * Date 2018年5月25日18点05分
 * function 权限认证注入
 */
public class AuthRealm extends AuthorizingRealm{

    @Autowired
    public UserService userService;
    private static Log log= LogFactory.getLog(AuthRealm.class);

    /**
     *
     *Author TateBrown
     * 权限注入,  可从DB中查询用户权限进行注入
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals){
        /**
         * 四类用户：1平台管理员，2SP开发管理员，3.SP运营管理员，4.终端用户
         * 权限较简单，管理用户对应角色如下
         * 1.admin,2,spDev,3,spOpe,4,endUser
         */
        String username=(String)principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo authorizationInfo =new SimpleAuthorizationInfo();
        return authorizationInfo;
    }

    /**
     * Author TateBrown
     * 登录认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException{
        String username=(String) token.getPrincipal();

        User user=null;
        try{
            user=userService.findByUserName(username);
        }catch(Exception e){
            e.printStackTrace();
        }
        if(user==null){
            throw new UnknownAccountException();
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                user.getUsername(), //用户名
                user.getPassword(), //密码,不用加密直接传入
                ByteSource.Util.bytes(user.getSalt()),
                getName()  //realm name
        );


        return authenticationInfo;
    }
    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }

}
